﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TsCore;

namespace TsWeb.Reports
{
    public partial class MatchesBySport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindSports();
            }
        }

        private void BindSports()
        {
            ComboBoxSports.DataSource = TsSystem.Instance.GetAllSports();
            ComboBoxSports.DataBind();
        }

        private void LoadDataGrid(Sport sport)
        {
            List<Match> matchesList = TsSystem.Instance.GetMatchesForSport(sport);
            GridViewMatches.DataSource = matchesList;
            GridViewMatches.DataBind();
        }



        protected void ButtonRunReport_Click(object sender, EventArgs e)
        {
            if (ComboBoxSports.SelectedItem != null)
            {
                Int32 sport = Int32.Parse(ComboBoxSports.SelectedItem.Value);
                Sport teamSport = TsSystem.Instance.GetSportWithTeams(sport);
                this.LoadDataGrid(teamSport);
            }
        }

        protected void GridViewMatches_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    int winner = Int32.Parse(e.Row.Cells[4].Text);

                    if (winner == -1)
                        e.Row.Cells[4].Text = "Partido sin jugar";
                    if (winner == 0)
                        e.Row.Cells[4].Text = "Empate";
                    if (winner != -1 && winner != 0)
                    {
                        e.Row.Cells[4].Text = TsSystem.Instance.GetTeamWithoutIncludes(winner).Name;
                    }


                }
            }
            catch (Exception)
            { }
        }


    }
}